package org.dromara.components.sysRegion.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.Tree;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.TreeBuildUtils;
import org.springframework.stereotype.Service;
import org.dromara.components.sysRegion.domain.bo.SysRegionBo;
import org.dromara.components.sysRegion.domain.vo.SysRegionVo;
import org.dromara.components.sysRegion.domain.SysRegion;
import org.dromara.components.sysRegion.mapper.SysRegionMapper;
import org.dromara.components.sysRegion.service.ISysRegionService;

import java.util.*;

/**
 * 省市区街道信息Service业务层处理
 *
 * @author wdm
 * @date 2024-02-26
 */
@RequiredArgsConstructor
@Service
public class SysRegionServiceImpl implements ISysRegionService {

    private final SysRegionMapper baseMapper;

    /**
     * 查询省市区街道信息
     */
    @Override
    public SysRegionVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }


    /**
     * 查询省市区街道信息列表
     */
    @Override
    public List<SysRegionVo> queryList(SysRegionBo bo) {
        return baseMapper.queryRegionVo(bo);
    }

    @Override
    public List<Tree<Long>> buildMenuTreeSelect(List<SysRegionVo> sysRegionVoList) {
        if (CollUtil.isEmpty(sysRegionVoList)) {
            return CollUtil.newArrayList();
        }
        return TreeBuildUtils.build(sysRegionVoList, (sysRegionVo, tree) ->
            tree.setId(sysRegionVo.getId())
                .setParentId(sysRegionVo.getParentId())
                .setName(sysRegionVo.getName())
                .setWeight(sysRegionVo.getId()));
    }

    private LambdaQueryWrapper<SysRegion> buildQueryWrapper(SysRegionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysRegion> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getParentId() != null, SysRegion::getParentId, bo.getParentId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysRegion::getName, bo.getName());
        lqw.eq(bo.getType() != null, SysRegion::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增省市区街道信息
     * @param bo
     * @return
     */
    @Override
    public Boolean insertByBo(SysRegionBo bo) {
        SysRegion add = MapstructUtils.convert(bo, SysRegion.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 批量新增省市区街道信息
     * @param boList
     * @return
     */
    @Override
    public Boolean insertBatchByBo(List<SysRegionBo> boList) {
        if(CollectionUtil.isNotEmpty(boList)){
            List<SysRegion> batchInsertList = new ArrayList<>();
            for (SysRegionBo bo:boList) {
                SysRegion add = MapstructUtils.convert(bo, SysRegion.class);
                validEntityBeforeSave(add);
                batchInsertList.add(add);
            }
            return baseMapper.insertBatch(batchInsertList);
        }
        return true;
    }

    /**
     * 修改省市区街道信息
     */
    @Override
    public Boolean updateByBo(SysRegionBo bo) {
        SysRegion update = MapstructUtils.convert(bo, SysRegion.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysRegion entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除省市区街道信息
     */
    @Override
    public Boolean deleteByType(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据类型删除
     * @param type
     * @return
     */
    @Override
    public Boolean deleteByType(int type) {
        LambdaQueryWrapper<SysRegion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRegion::getType,type);
        return baseMapper.delete(queryWrapper) > 0;
    }

    @Override
    public Boolean deleteByParentId(Long parentId) {
        LambdaQueryWrapper<SysRegion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRegion::getParentId,parentId);
        return baseMapper.delete(queryWrapper) > 0;
    }
}
